import DEFAULTS from './defaults.js'
import {serialize,addURLData,serializeJSON} from './utils.js'
import {HTTP_GET,
  CONTENT_TYPE_FROM_URLENCODED,
  CONTENT_TYPE_JSON} 
from './constants.js'
class Ajax{
  constructor(url,options){
    this.url=url;
    this.options=Object.assign({},DEFAULTS,options)
    this.init()
  }
  //初始化
  init(){
    const xhr=new XMLHttpRequest()
    this.xhr=xhr;
    this.bindEvents();
    xhr.open(this.options.method,this.url+this.addParam(),true)
    //设置resoponseType
    this.setResponseType()
    //设置跨域是否携带cookie
    this.setCookie()
    //设置超时
    this.setTimeout()
    //发送请求
    this.sendData()

    
  }
  //绑定响应事件处理程序
  bindEvents(){
    const xhr=this.xhr;
    const {success,httpCodeError,error,abort,timeout}=this.options
    xhr.onload=()=>{
      if (this.isok()) {
        success(xhr.response,xhr)
      }else{
        httpCodeError(xhr.status,xhr)
      }
    }
    xhr.onerror=function(){
      error(xhr)
    }
    xhr.onabort=function(){
      abort(xhr)
    }
    xhr.ontimeout=function(){
      timeout(xhr)
    }

  }
  //检测响应的http状态码是否正常
  isok(){
    const xhr=this.xhr;
    return (xhr.status >= 200 && xhr.status < 300) || xhr.status === 304;
  }
  //在地址上添加数据
  addParam(){
    const {params}=this.options;
    if(!params) return'';
    return addURLData(this.url,serialize(params))
  }
  setResponseType(){
    this.xhr.responseType=this.options.responseType
  }
  setCookie(){
    if(this.options.withCredentials){
      this.xhr.withCredentials=true
    }
  }
  setTimeout(){
    const {timeoutTime}=this.options
    if(timeoutTime>0){
      this.xhr.timeout=timeoutTime
    }
  }
  sendData(){
    const xhr=this.xhr;
    if(!this.isSendData()){
      return xhr.send(null)
    }
    let resultData=null
    const {data}=this.options
    if(this.isFormURLEncodedData()){
      this.setContentType(CONTENT_TYPE_FROM_URLENCODED)
      resultData=serialize(data)
    }else if(this.isJSONData()){
      this.setContentType(CONTENT_TYPE_JSON)
      resultData=serializeJSON(data)
    }else if(this.isFormData){
      resultData=data;
    }else{
      this.setContentType()
      resultData=data
    }
    xhr.send(resultData)
   
  }
  //是否需要使用send发送数据
  isSendData(){
    const {data,method}=this.options;
    if(!data) return false;
    if(method.toUpperCase()===HTTP_GET)return false;
    return true
  }
  //是否发送FormData格式的数据
  isFormData(){
    return this.options.data instanceof FormData;
  }
  //是否发送application/x-www-form-urlencoded格式的数据
  isFormURLEncodedData(){
    return this.options.contentType.toLowerCase().includes(CONTENT_TYPE_FROM_URLENCODED)
  }
  //是否发送application/josn格式的数据
  isJSONData(){
    return this.options.contentType.toLowerCase().includes(CONTENT_TYPE_JSON)
  }
  //设置contenet-Type
  setContentType(contentType=this.options.contentType){
    if(!contentType)return;
    this.xhr.setRequestHeader('Content-Type',contentType);
    console.log(contentType);
  }
  //获取xhr对象
  getXHR(){
    return this.xhr
  }
}
export default Ajax